package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.xthings.constant.HardwareTypeEnum;
import com.workcell.workdata.xthings.dto.CodeChainnumVo;
import com.workcell.workdata.xthings.dto.ExcelExportTemplateVo;
import com.workcell.workdata.xthings.dto.ExcelImportTemplateVo;
import com.workcell.workdata.xthings.entity.BaseBeltScales;
import com.workcell.workdata.xthings.entity.BaseCamera;
import com.workcell.workdata.xthings.entity.BaseFlowmeter;
import com.workcell.workdata.xthings.entity.BaseFreezer;
import com.workcell.workdata.xthings.entity.BaseHeatExchangers;
import com.workcell.workdata.xthings.entity.BaseLevelGauge;
import com.workcell.workdata.xthings.entity.BaseNaturalGas;
import com.workcell.workdata.xthings.entity.BasePowermeter;
import com.workcell.workdata.xthings.entity.BaseRotaryKiln;
import com.workcell.workdata.xthings.entity.BaseSerialDevice;
import com.workcell.workdata.xthings.entity.BaseSmartTanks;
import com.workcell.workdata.xthings.entity.BaseSoundLightAlarm;
import com.workcell.workdata.xthings.entity.BaseTemperatureHumidity;
import com.workcell.workdata.xthings.entity.BaseTimeClock;
import com.workcell.workdata.xthings.entity.BaseTvBox;
import com.workcell.workdata.xthings.entity.BaseUpperComputer;
import com.workcell.workdata.xthings.entity.BaseValve;
import com.workcell.workdata.xthings.entity.BaseVoiceSynthesis;
import com.workcell.workdata.xthings.entity.BaseWaterMeter;
import com.workcell.workdata.xthings.service.CodeChainnumService;
import com.workcell.workdata.xthings.service.CodeExcelService;
import com.workcell.workdata.xthings.service.factory.CodeBaseFactory;
import com.workcell.workdata.xthings.utils.ObjectUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.workcell.workdata.xthings.service.impl.DeviceBindServiceImpl.DEVICE_ADDRESS_MAX;
import static com.workcell.workdata.xthings.service.impl.DeviceBindServiceImpl.DEVICE_ADDRESS_MIN;

/**
 * @Author 廖博文
 * @Date 2022/12/5 10:38
 * @Description 二维码Excel操作类
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class CodeExcelServiceImpl implements CodeExcelService {

    private final CodeChainnumService codeChainnumService;
    private final CodeBaseFactory codeBaseFactory;

    /**
     * 导出二维码导入模板
     *
     * @param baseId 硬件ID
     * @param num    生成数量
     * @return
     */
    @Override
    public Object exportTemplate(String baseId, String type, int num) {
        // 获取硬件信息
        switch (HardwareTypeEnum.getByCode(type)) {
            case FLOW_METER:
                List<BaseFlowmeter> baseFlowmeterList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseFlowmeter.class);
                if (CollectionUtils.isNotEmpty(baseFlowmeterList)) {
                    // 复制数据创建导出集合
                    BaseFlowmeter baseFlowmeter = baseFlowmeterList.get(0);
                    ExcelExportTemplateVo.FlowMeterVo flowMeterVo = new ExcelExportTemplateVo.FlowMeterVo();
                    BeanUtil.copyProperties(baseFlowmeter, flowMeterVo);
                    List<ExcelExportTemplateVo.FlowMeterVo> flowMeterVoList = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        flowMeterVoList.add(flowMeterVo);
                    }
                    return flowMeterVoList;
                }
                break;
            case POWER_METER:
                List<BasePowermeter> basePowermeterList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BasePowermeter.class);
                if (CollectionUtils.isNotEmpty(basePowermeterList)) {
                    // 复制数据创建导出集合
                    BasePowermeter basePowermeter = basePowermeterList.get(0);
                    ExcelExportTemplateVo.PowerMeterVo powerMeterVo = new ExcelExportTemplateVo.PowerMeterVo();
                    BeanUtil.copyProperties(basePowermeter, powerMeterVo);
                    List<ExcelExportTemplateVo.PowerMeterVo> powerMeterVoList = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        powerMeterVoList.add(powerMeterVo);
                    }
                    return powerMeterVoList;
                }
                break;
            case TV_BOX:
                List<BaseTvBox> baseTvBoxList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseTvBox.class);
                if (CollectionUtils.isNotEmpty(baseTvBoxList)) {
                    // 复制数据创建导出集合
                    BaseTvBox baseTvBox = baseTvBoxList.get(0);
                    ExcelExportTemplateVo.BaseTvBoxVo baseTvBoxVo = new ExcelExportTemplateVo.BaseTvBoxVo();
                    BeanUtil.copyProperties(baseTvBox, baseTvBoxVo);
                    List<ExcelExportTemplateVo.BaseTvBoxVo> baseTvBoxVoList = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        baseTvBoxVoList.add(baseTvBoxVo);
                    }
                    return baseTvBoxVoList;
                }
                break;
            //打卡机
            case TIME_CLOCK:
                List<BaseTimeClock> baseTimeClockList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseTimeClock.class);
                if (CollectionUtils.isNotEmpty(baseTimeClockList)) {
                    // 复制数据创建导出集合
                    BaseTimeClock baseTimeClock = baseTimeClockList.get(0);
                    ExcelExportTemplateVo.BaseTimeClockVo baseTimeClockVo = new ExcelExportTemplateVo.BaseTimeClockVo();
                    BeanUtil.copyProperties(baseTimeClock, baseTimeClockVo);
                    List<ExcelExportTemplateVo.BaseTimeClockVo> baseTimeClockVoList = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        baseTimeClockVoList.add(baseTimeClockVo);
                    }
                    return baseTimeClockVoList;
                }
                break;
            case CAMERA:
                List<BaseCamera> BaseCameraList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseCamera.class);
                if (CollectionUtils.isNotEmpty(BaseCameraList)) {
                    // 复制数据创建导出集合
                    BaseCamera baseCamera = BaseCameraList.get(0);
                    ExcelExportTemplateVo.BaseCameraVo baseCameraVo = new ExcelExportTemplateVo.BaseCameraVo();
                    BeanUtil.copyProperties(baseCamera, baseCameraVo);
                    List<ExcelExportTemplateVo.BaseCameraVo> baseTimeClockVoList = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        baseTimeClockVoList.add(baseCameraVo);
                    }
                    return baseTimeClockVoList;
                }
                break;
            case SERIAL_DEVICE:
                List<BaseSerialDevice> baseSerialDevices = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseSerialDevice.class);
                if (CollectionUtils.isNotEmpty(baseSerialDevices)) {
                    // 复制数据创建导出集合
                    BaseSerialDevice baseSerialDevice = baseSerialDevices.get(0);
                    ExcelExportTemplateVo.BaseSerialDeviceVo baseSerialDeviceVo = new ExcelExportTemplateVo.BaseSerialDeviceVo();
                    BeanUtil.copyProperties(baseSerialDevice, baseSerialDeviceVo);
                    List<ExcelExportTemplateVo.BaseSerialDeviceVo> baseSerialDeviceVoList = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        baseSerialDeviceVoList.add(baseSerialDeviceVo);
                    }
                    return baseSerialDeviceVoList;
                }
                break;
            case BELT_SCALES:
                List<BaseBeltScales> baseBeltScalesList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseBeltScales.class);
                if (CollectionUtils.isNotEmpty(baseBeltScalesList)) {
                    // 复制数据创建导出集合
                    BaseBeltScales baseBeltScale = baseBeltScalesList.get(0);
                    ExcelExportTemplateVo.BaseBaseBeltScalesVo baseBaseBeltScalesVo = new ExcelExportTemplateVo.BaseBaseBeltScalesVo();
                    BeanUtil.copyProperties(baseBeltScale, baseBaseBeltScalesVo);
                    List<ExcelExportTemplateVo.BaseBaseBeltScalesVo> baseSerialDeviceVoList = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        baseSerialDeviceVoList.add(baseBaseBeltScalesVo);
                    }
                    return baseSerialDeviceVoList;
                }
                break;
            case TEMPERATURE_HUMIDITY:
                List<BaseTemperatureHumidity> temperatureHumidityList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseTemperatureHumidity.class);
                if (CollectionUtils.isNotEmpty(temperatureHumidityList)) {
                    // 复制数据创建导出集合
                    BaseTemperatureHumidity baseTemperatureHumidity = temperatureHumidityList.get(0);
                    ExcelExportTemplateVo.BaseTemperatureHumidityVo baseTemperatureHumidityVo = new ExcelExportTemplateVo.BaseTemperatureHumidityVo();
                    BeanUtil.copyProperties(baseTemperatureHumidity, baseTemperatureHumidityVo);
                    List<ExcelExportTemplateVo.BaseTemperatureHumidityVo> baseTemperatureHumidityVoList = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        baseTemperatureHumidityVoList.add(baseTemperatureHumidityVo);
                    }
                    return baseTemperatureHumidityVoList;
                }
                break;
            case FREEZER:
                List<BaseFreezer> baseFreezerList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseFreezer.class);
                if (CollectionUtils.isNotEmpty(baseFreezerList)) {
                    // 复制数据创建导出集合
                    BaseFreezer baseFreezer = baseFreezerList.get(0);
                    ExcelExportTemplateVo.BaseFreezerVo baseFreezerVo = new ExcelExportTemplateVo.BaseFreezerVo();
                    BeanUtil.copyProperties(baseFreezer, baseFreezerVo);
                    List<ExcelExportTemplateVo.BaseFreezerVo> baseFreezerVoList = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        baseFreezerVoList.add(baseFreezerVo);
                    }
                    return baseFreezerVoList;
                }
                break;
            case HEAT_EXCHANGERS:
                List<BaseHeatExchangers> baseHeatExchangersList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseHeatExchangers.class);
                if (CollectionUtils.isNotEmpty(baseHeatExchangersList)) {
                    // 复制数据创建导出集合
                    BaseHeatExchangers baseHeatExchangers = baseHeatExchangersList.get(0);
                    ExcelExportTemplateVo.BaseHeatExchangersVo baseHeatExchangersVo = new ExcelExportTemplateVo.BaseHeatExchangersVo();
                    BeanUtil.copyProperties(baseHeatExchangers, baseHeatExchangersVo);
                    List<ExcelExportTemplateVo.BaseHeatExchangersVo> baseHeatExchangersVoList = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        baseHeatExchangersVoList.add(baseHeatExchangersVo);
                    }
                    return baseHeatExchangersVoList;
                }
                break;
            case LEVEL_GAUGE:
                List<BaseLevelGauge> baseLevelGaugeList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseLevelGauge.class);
                if (CollectionUtils.isNotEmpty(baseLevelGaugeList)) {
                    // 复制数据创建导出集合
                    BaseLevelGauge baseLevelGauge = baseLevelGaugeList.get(0);
                    ExcelExportTemplateVo.BaseLevelGaugeVo baseLevelGaugeVo = new ExcelExportTemplateVo.BaseLevelGaugeVo();
                    BeanUtil.copyProperties(baseLevelGauge, baseLevelGaugeVo);
                    List<ExcelExportTemplateVo.BaseLevelGaugeVo> baseLevelGaugeVos = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        baseLevelGaugeVos.add(baseLevelGaugeVo);
                    }
                    return baseLevelGaugeVos;
                }
                break;
            case VALVE:
                List<BaseValve> baseValveList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseValve.class);
                if (CollectionUtils.isNotEmpty(baseValveList)) {
                    // 复制数据创建导出集合
                    BaseValve baseValve = baseValveList.get(0);
                    ExcelExportTemplateVo.BaseValveVo baseValveVo = new ExcelExportTemplateVo.BaseValveVo();
                    BeanUtil.copyProperties(baseValve, baseValveVo);
                    List<ExcelExportTemplateVo.BaseValveVo> baseValveVos = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        baseValveVos.add(baseValveVo);
                    }
                    return baseValveVos;
                }
                break;
            case NATURAL_GAS:
                List<BaseNaturalGas> baseList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseNaturalGas.class);
                if (CollectionUtils.isNotEmpty(baseList)) {
                    // 复制数据创建导出集合
                    BaseNaturalGas baseNaturalGas = baseList.get(0);
                    ExcelExportTemplateVo.BaseValveVo baseValveVo = new ExcelExportTemplateVo.BaseValveVo();
                    BeanUtil.copyProperties(baseNaturalGas, baseValveVo);
                    List<ExcelExportTemplateVo.BaseValveVo> baseValveVos = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        baseValveVos.add(baseValveVo);
                    }
                    return baseValveVos;
                }
                break;
            case UPPER_COMPUTER:
                List<BaseUpperComputer> baseUpperComputerList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseUpperComputer.class);
                if (CollectionUtils.isNotEmpty(baseUpperComputerList)) {
                    // 复制数据创建导出集合
                    BaseUpperComputer baseUpperComputer = baseUpperComputerList.get(0);
                    ExcelExportTemplateVo.BaseUpperComputerVo baseUpperComputerVo = new ExcelExportTemplateVo.BaseUpperComputerVo();
                    BeanUtil.copyProperties(baseUpperComputer, baseUpperComputerVo);
                    List<ExcelExportTemplateVo.BaseUpperComputerVo> baseUpperComputerVos = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        baseUpperComputerVos.add(baseUpperComputerVo);
                    }
                    return baseUpperComputerVos;
                }
                break;
            case SMART_TANKS:
                List<BaseSmartTanks> baseSmartTanksList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseSmartTanks.class);
                if (CollectionUtils.isNotEmpty(baseSmartTanksList)) {
                    // 复制数据创建导出集合
                    BaseSmartTanks baseSmartTanks = baseSmartTanksList.get(0);
                    ExcelExportTemplateVo.BaseSmartTanksVo baseSmartTanksVo = new ExcelExportTemplateVo.BaseSmartTanksVo();
                    BeanUtil.copyProperties(baseSmartTanks, baseSmartTanksVo);
                    List<ExcelExportTemplateVo.BaseSmartTanksVo> baseSmartTanksVos = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        baseSmartTanksVos.add(baseSmartTanksVo);
                    }
                    return baseSmartTanksVos;
                }
                break;
            case ROTARY_KILN:
                List<BaseRotaryKiln> baseRotaryKilnsList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseRotaryKiln.class);
                if (CollectionUtils.isNotEmpty(baseRotaryKilnsList)) {
                    // 复制数据创建导出集合
                    BaseRotaryKiln baseRotaryKilns = baseRotaryKilnsList.get(0);
                    ExcelExportTemplateVo.BaseRotaryKilnVo baseRotaryKilnVo = new ExcelExportTemplateVo.BaseRotaryKilnVo();
                    BeanUtil.copyProperties(baseRotaryKilns, baseRotaryKilnVo);
                    List<ExcelExportTemplateVo.BaseRotaryKilnVo> baseRotaryKilnVos = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        baseRotaryKilnVos.add(baseRotaryKilnVo);
                    }
                    return baseRotaryKilnVos;
                }
                break;
            case WATER_METER:
                List<BaseWaterMeter> baseWaterMeterList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseWaterMeter.class);
                if (CollectionUtils.isNotEmpty(baseWaterMeterList)) {
                    // 复制数据创建导出集合
                    BaseWaterMeter baseWaterMeter = baseWaterMeterList.get(0);
                    ExcelExportTemplateVo.BaseWaterMeterVo baseWaterMeterVo = new ExcelExportTemplateVo.BaseWaterMeterVo();
                    BeanUtil.copyProperties(baseWaterMeter, baseWaterMeterVo);
                    List<ExcelExportTemplateVo.BaseWaterMeterVo> baseWaterMeterVos = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        baseWaterMeterVos.add(baseWaterMeterVo);
                    }
                    return baseWaterMeterVos;
                }
                break;
            case VOICE_SYNTHESIS:
                List<BaseVoiceSynthesis> baseVoiceSynthesisList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseVoiceSynthesis.class);
                if (CollectionUtils.isNotEmpty(baseVoiceSynthesisList)) {
                    // 复制数据创建导出集合
                    BaseVoiceSynthesis baseVoiceSynthesis = baseVoiceSynthesisList.get(0);
                    ExcelExportTemplateVo.BaseVoiceSynthesisVo baseVoiceSynthesisVo = new ExcelExportTemplateVo.BaseVoiceSynthesisVo();
                    BeanUtil.copyProperties(baseVoiceSynthesis, baseVoiceSynthesisVo);
                    List<ExcelExportTemplateVo.BaseVoiceSynthesisVo> baseVoiceSynthesisVos = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        baseVoiceSynthesisVos.add(baseVoiceSynthesisVo);
                    }
                    return baseVoiceSynthesisVos;
                }
                break;
            case SOUND_LIGHT_ALARM:
                List<BaseSoundLightAlarm> baseSoundLightAlarmList = ObjectUtils.castList(codeBaseFactory.getHardwareBase(baseId, type), BaseSoundLightAlarm.class);
                if (CollectionUtils.isNotEmpty(baseSoundLightAlarmList)) {
                    // 复制数据创建导出集合
                    BaseSoundLightAlarm baseSoundLightAlarm = baseSoundLightAlarmList.get(0);
                    ExcelExportTemplateVo.BaseSoundLightAlarmVo baseSoundLightAlarmVo = new ExcelExportTemplateVo.BaseSoundLightAlarmVo();
                    BeanUtil.copyProperties(baseSoundLightAlarm, baseSoundLightAlarmVo);
                    List<ExcelExportTemplateVo.BaseSoundLightAlarmVo> baseSoundLightAlarmVoList = new ArrayList<>(num);
                    for (int i = 0; i < num; i++) {
                        baseSoundLightAlarmVoList.add(baseSoundLightAlarmVo);
                    }
                    return baseSoundLightAlarmVoList;
                }
                break;
        }
        return new ArrayList<>();
    }

    /**
     * 导入流量计信息
     *
     * @param excelVOList
     * @return
     */
    @Override
    public R importFlowMeterCodeChainnum(List<ExcelImportTemplateVo.FlowMeterVo> excelVOList) {
        // 获取当前所有流量计信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.FLOW_METER.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.FlowMeterVo flowMeterVo = excelVOList.get(i);
            String deviceNo = flowMeterVo.getDeviceNo();
            String manufacturerCode = flowMeterVo.getManufacturerCode();
            String fmId = flowMeterVo.getFmId();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(flowMeterVo.getFmId());
            excelImportChainNumResult.setHardwareName(flowMeterVo.getFmName());
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }
                // 校验是否存在该设备
                JSONObject hardware = hardwareList.stream().filter(j -> j.get("baseId").equals(fmId)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(hardware)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = hardware.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(fmId);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.FLOW_METER.getCode());
                saveChainNumVo.setTenantCode(flowMeterVo.getTenantCode());
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(fmId);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);
    }

    /**
     * 导入电压计信息
     *
     * @param excelVOList
     * @return
     */
    @Override
    public R importPowerMeterCodeChainnum(List<ExcelImportTemplateVo.PowerMeterVo> excelVOList) {
        // 获取当前所有流量计信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.POWER_METER.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.PowerMeterVo powerMeterVo = excelVOList.get(i);
            String deviceNo = powerMeterVo.getDeviceNo();
            String manufacturerCode = powerMeterVo.getManufacturerCode();
            String addressId = powerMeterVo.getAddressId();
            String pmId = powerMeterVo.getPmId();
            String baudRate = powerMeterVo.getBaudRate();
            String commDataFormat = powerMeterVo.getCommDataFormat();
            String tenantCode = powerMeterVo.getTenantCode();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(powerMeterVo.getPmId());
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(addressId)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的机器码为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(baudRate)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的波特率为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(commDataFormat)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的通讯数据格式为空。");
                    continue;
                }

                Integer addressIdInt = Integer.parseInt(addressId);
                if (addressIdInt < DEVICE_ADDRESS_MIN || addressIdInt > DEVICE_ADDRESS_MAX) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的机器码不在范围" + DEVICE_ADDRESS_MIN + "-" + DEVICE_ADDRESS_MAX);
                    continue;
                }
                // 校验是否存在该设备
                JSONObject hardware = hardwareList.stream().filter(j -> j.get("baseId").equals(pmId)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(hardware)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = hardware.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(pmId);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.POWER_METER.getCode());
                saveChainNumVo.setAddressId(addressId);
                saveChainNumVo.setBaudRate(Integer.parseInt(baudRate));
                saveChainNumVo.setCommDataFormat(commDataFormat);
                saveChainNumVo.setTenantCode(tenantCode);
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(pmId);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }

        }
        return R.ok(excelImportChainNumResultList);
    }

    /**
     * 导入电视盒子二维码信息
     *
     * @param excelVOList
     * @return
     */
    @Override
    public R importTvBoxCodeChainnum(List<ExcelImportTemplateVo.TvBoxVo> excelVOList) {
        // 获取当前所有电视盒子信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.TV_BOX.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.TvBoxVo tvBoxVo = excelVOList.get(i);
            String deviceNo = tvBoxVo.getDeviceNo();
            String manufacturerCode = tvBoxVo.getManufacturerCode();
            String id = tvBoxVo.getId();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(id);
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }
                // 校验是否存在该设备
                JSONObject hardware = hardwareList.stream().filter(j -> j.get("baseId").equals(id)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(hardware)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = hardware.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(id);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.TV_BOX.getCode());
                saveChainNumVo.setTenantCode(tvBoxVo.getTenantCode());
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(id);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);
    }

    @Override
    public R importSerialDeviceCodeChainnum(List<ExcelImportTemplateVo.SerialDeviceVo> excelVOList) {
        // 获取当前所有串口设备信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.SERIAL_DEVICE.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        for (int i = 1; i < excelVOList.size(); i++) {
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            ExcelImportTemplateVo.SerialDeviceVo serialDeviceVo = excelVOList.get(i);
            String deviceNo = serialDeviceVo.getDeviceNo();
            String manufacturerCode = serialDeviceVo.getManufacturerCode();
            String pmId = serialDeviceVo.getDtId();
            String macAddress = serialDeviceVo.getMacAddress();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(serialDeviceVo.getDtId());
            excelImportChainNumResult.setFlag(false);

            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }

                if (ObjectUtil.isEmpty(macAddress)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的MAC地址为空。");
                    continue;
                }

                // 校验是否存在该设备
                JSONObject hardware = hardwareList.stream().filter(j -> j.get("baseId").equals(pmId)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(hardware)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = hardware.get("hardwareName").toString();

                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(pmId);
                saveChainNumVo.setMacAddress(macAddress);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.SERIAL_DEVICE.getCode());
                saveChainNumVo.setTenantCode(serialDeviceVo.getTenantCode());

                saveChainNumVo.setBaudRate(Integer.parseInt(ObjectUtil.defaultIfNull(serialDeviceVo.getBaudRate(), "9600")));
                saveChainNumVo.setWorkingMode(serialDeviceVo.getWorkingMode());
                saveChainNumVo.setPublishTopic(serialDeviceVo.getPublishTopic());
                saveChainNumVo.setSubscribeTopic(serialDeviceVo.getSubscribeTopic());

                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(pmId);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);
    }


    @Override
    public R importTimeClockCodeChainnum(List<ExcelImportTemplateVo.TimeClockVo> excelVOList) {
        // 获取当前所有打卡机信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.TIME_CLOCK.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.TimeClockVo timeClockVoVo = excelVOList.get(i);
            String deviceNo = timeClockVoVo.getDeviceNo();
            String manufacturerCode = timeClockVoVo.getManufacturerCode();
            String id = timeClockVoVo.getId();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(id);
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }
                // 校验是否存在该设备
                JSONObject hardware = hardwareList.stream().filter(j -> j.get("baseId").equals(id)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(hardware)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = hardware.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(id);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.TIME_CLOCK.getCode());
                saveChainNumVo.setTenantCode(timeClockVoVo.getTenantCode());
                saveChainNumVo.setIp(timeClockVoVo.getIp());
                saveChainNumVo.setPort(timeClockVoVo.getPort());
                saveChainNumVo.setTimeClockIp(timeClockVoVo.getTimeClockIp());
                saveChainNumVo.setUserName(timeClockVoVo.getUserName());
                saveChainNumVo.setPassword(timeClockVoVo.getPassword());
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(id);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);
    }

    @Override
    public R importCameraCodeChainnum(List<ExcelImportTemplateVo.CameraVo> excelVOList) {

        // 获取当前所有摄像头信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.CAMERA.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.CameraVo cameraVo = excelVOList.get(i);
            String deviceNo = cameraVo.getDeviceNo();
            String manufacturerCode = cameraVo.getManufacturerCode();
            String id = cameraVo.getId();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(id);
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }
                // 校验是否存在该设备
                JSONObject hardware = hardwareList.stream().filter(j -> j.get("baseId").equals(id)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(hardware)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = hardware.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(id);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.CAMERA.getCode());
                saveChainNumVo.setTenantCode(cameraVo.getTenantCode());
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(id);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);
    }

    @Override
    public R importBeltScalesCodeChainnum(List<ExcelImportTemplateVo.BeltScales> excelVOList) {

        // 获取当前所有皮带秤信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.BELT_SCALES.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.BeltScales beltScales = excelVOList.get(i);
            String deviceNo = beltScales.getDeviceNo();
            String manufacturerCode = beltScales.getManufacturerCode();
            String id = beltScales.getId();
            String addressId = beltScales.getAddressId();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(id);
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(addressId)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的机器码为空。");
                    continue;
                }

                Integer addressIdInt = Integer.parseInt(addressId);
                if (addressIdInt < DEVICE_ADDRESS_MIN || addressIdInt > DEVICE_ADDRESS_MAX) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的机器码不在范围" + DEVICE_ADDRESS_MIN + "-" + DEVICE_ADDRESS_MAX);
                    continue;
                }
                // 校验是否存在该设备
                JSONObject hardware = hardwareList.stream().filter(j -> j.get("baseId").equals(id)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(hardware)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = hardware.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(id);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.BELT_SCALES.getCode());
                saveChainNumVo.setAddressId(addressId);
                saveChainNumVo.setTenantCode(beltScales.getTenantCode());
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(id);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);
    }

    @Override
    public R importFreezerCodeChainnum(List<ExcelImportTemplateVo.Freezer> excelVOList) {

        // 获取当前所有冷冻机信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.FREEZER.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.Freezer freezer = excelVOList.get(i);
            String deviceNo = freezer.getDeviceNo();
            String manufacturerCode = freezer.getManufacturerCode();
            String id = freezer.getId();
            String addressId = freezer.getAddressId();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(id);
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(addressId)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的机器码为空。");
                    continue;
                }

                Integer addressIdInt = Integer.parseInt(addressId);
                if (addressIdInt < DEVICE_ADDRESS_MIN || addressIdInt > DEVICE_ADDRESS_MAX) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的机器码不在范围" + DEVICE_ADDRESS_MIN + "-" + DEVICE_ADDRESS_MAX);
                    continue;
                }
                // 校验是否存在该设备
                JSONObject hardware = hardwareList.stream().filter(j -> j.get("baseId").equals(id)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(hardware)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = hardware.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(id);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.FREEZER.getCode());
                saveChainNumVo.setAddressId(addressId);
                saveChainNumVo.setTenantCode(freezer.getTenantCode());
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(id);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);
    }

    @Override
    public R importHeatExchangersCodeChainnum(List<ExcelImportTemplateVo.HeatExchangers> excelVOList) {

        // 获取当前所有换热机信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.HEAT_EXCHANGERS.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.HeatExchangers heatExchangers = excelVOList.get(i);
            String deviceNo = heatExchangers.getDeviceNo();
            String manufacturerCode = heatExchangers.getManufacturerCode();
            String id = heatExchangers.getId();
            String addressId = heatExchangers.getAddressId();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(id);
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(addressId)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的机器码为空。");
                    continue;
                }

                Integer addressIdInt = Integer.parseInt(addressId);
                if (addressIdInt < DEVICE_ADDRESS_MIN || addressIdInt > DEVICE_ADDRESS_MAX) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的机器码不在范围" + DEVICE_ADDRESS_MIN + "-" + DEVICE_ADDRESS_MAX);
                    continue;
                }
                // 校验是否存在该设备
                JSONObject hardware = hardwareList.stream().filter(j -> j.get("baseId").equals(id)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(hardware)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = hardware.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(id);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.HEAT_EXCHANGERS.getCode());
                saveChainNumVo.setAddressId(addressId);
                saveChainNumVo.setTenantCode(heatExchangers.getTenantCode());
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(id);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);
    }

    @Override
    public R importTemperatureHumidityCodeChainnum(List<ExcelImportTemplateVo.TemperatureHumidityVo> excelVOList) {
        // 获取当前所有温湿度计信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.TEMPERATURE_HUMIDITY.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.TemperatureHumidityVo temperatureHumidityVo = excelVOList.get(i);
            String deviceNo = temperatureHumidityVo.getDeviceNo();
            String manufacturerCode = temperatureHumidityVo.getManufacturerCode();
            String id = temperatureHumidityVo.getId();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(id);
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }
                // 校验是否存在该设备
                JSONObject hardware = hardwareList.stream().filter(j -> j.get("baseId").equals(id)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(hardware)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = hardware.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(id);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.TEMPERATURE_HUMIDITY.getCode());
                saveChainNumVo.setTenantCode(temperatureHumidityVo.getTenantCode());
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(id);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);
    }

    @Override
    public R importLeveGaugeChainnum(List<ExcelImportTemplateVo.LeveGaugeClockVo> excelVOList) {
        // 获取当前所有液位计信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.LEVEL_GAUGE.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.LeveGaugeClockVo leveGauge = excelVOList.get(i);
            String deviceNo = leveGauge.getDeviceNo();
            String manufacturerCode = leveGauge.getManufacturerCode();
            String id = leveGauge.getId();
            String addressId = leveGauge.getAddressId();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(id);
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(addressId)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的机器码为空。");
                    continue;
                }

                Integer addressIdInt = Integer.parseInt(addressId);
                if (addressIdInt < DEVICE_ADDRESS_MIN || addressIdInt > DEVICE_ADDRESS_MAX) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的机器码不在范围" + DEVICE_ADDRESS_MIN + "-" + DEVICE_ADDRESS_MAX);
                    continue;
                }
                // 校验是否存在该设备
                JSONObject leveGaugeJson = hardwareList.stream().filter(j -> j.get("baseId").equals(id)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(leveGaugeJson)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = leveGaugeJson.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(id);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.LEVEL_GAUGE.getCode());
                saveChainNumVo.setAddressId(addressId);
                saveChainNumVo.setTenantCode(leveGauge.getTenantCode());
                saveChainNumVo.setAlias(leveGauge.getAlias());
                saveChainNumVo.setDiameter(leveGauge.getDiameter());
                saveChainNumVo.setTotalVolume(leveGauge.getTotalVolume());
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(id);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);
    }

    @Override
    public R importValveChainnum(List<ExcelImportTemplateVo.ValveVo> excelVOList) {
        // 获取当前所有阀门信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.VALVE.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.ValveVo valveVo = excelVOList.get(i);
            String deviceNo = valveVo.getDeviceNo();
            String manufacturerCode = valveVo.getManufacturerCode();
            String id = valveVo.getId();
            String addressId = valveVo.getAddressId();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(id);
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }
                if (ObjectUtil.isNotNull(addressId)) {
                    Integer addressIdInt = Integer.parseInt(addressId);
                    if (addressIdInt < DEVICE_ADDRESS_MIN || addressIdInt > DEVICE_ADDRESS_MAX) {
                        excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的机器码不在范围" + DEVICE_ADDRESS_MIN + "-" + DEVICE_ADDRESS_MAX);
                        continue;
                    }
                }


                // 校验是否存在该设备
                JSONObject valveJson = hardwareList.stream().filter(j -> j.get("baseId").equals(id)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(valveJson)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = valveJson.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(id);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.VALVE.getCode());
                saveChainNumVo.setAddressId(addressId);
                saveChainNumVo.setTenantCode(valveVo.getTenantCode());
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(id);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);
    }

    @Override
    public R importNaturalGasChainnum(List<ExcelImportTemplateVo.NaturalGasVo> excelVOList) {
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.NATURAL_GAS.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.NaturalGasVo naturalGasVo = excelVOList.get(i);
            String deviceNo = naturalGasVo.getDeviceNo();
            String manufacturerCode = naturalGasVo.getManufacturerCode();
            String id = naturalGasVo.getId();
            String addressId = naturalGasVo.getAddressId();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(id);
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(addressId)) {
                    Integer addressIdInt = Integer.parseInt(addressId);
                    if (addressIdInt < DEVICE_ADDRESS_MIN || addressIdInt > DEVICE_ADDRESS_MAX) {
                        excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的机器码不在范围" + DEVICE_ADDRESS_MIN + "-" + DEVICE_ADDRESS_MAX);
                        continue;
                    }
                }


                // 校验是否存在该设备
                JSONObject gasJson = hardwareList.stream().filter(j -> j.get("baseId").equals(id)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(gasJson)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = gasJson.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(id);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.NATURAL_GAS.getCode());
                saveChainNumVo.setAddressId(addressId);
                saveChainNumVo.setTenantCode(naturalGasVo.getTenantCode());
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(id);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);

    }

    @Override
    public R importUpperComputerChainnum(List<ExcelImportTemplateVo.UpperComputerVo> excelVOList) {
        // 获取当前所有上位机信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.UPPER_COMPUTER.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.UpperComputerVo upperComputerVo = excelVOList.get(i);
            String deviceNo = upperComputerVo.getDeviceNo();
            String manufacturerCode = upperComputerVo.getManufacturerCode();
            String id = upperComputerVo.getId();
            String addressId = upperComputerVo.getAddressId();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(id);
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }

                // 校验是否存在该设备
                JSONObject valveJson = hardwareList.stream().filter(j -> j.get("baseId").equals(id)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(valveJson)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = valveJson.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(id);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.UPPER_COMPUTER.getCode());
                saveChainNumVo.setAddressId(addressId);
                saveChainNumVo.setTenantCode(upperComputerVo.getTenantCode());
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(id);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);
    }

    @Override
    public R importSmartTanksChainnum(List<ExcelImportTemplateVo.SmartTanksVo> excelVOList) {
        // 获取当前所有上位机信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.SMART_TANKS.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.SmartTanksVo smartTanksVo = excelVOList.get(i);
            String deviceNo = smartTanksVo.getDeviceNo();
            String manufacturerCode = smartTanksVo.getManufacturerCode();
            String id = smartTanksVo.getId();
            String addressId = smartTanksVo.getAddressId();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(id);
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }


                // 校验是否存在该设备
                JSONObject valveJson = hardwareList.stream().filter(j -> j.get("baseId").equals(id)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(valveJson)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = valveJson.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(id);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.SMART_TANKS.getCode());
                saveChainNumVo.setAddressId(addressId);
                saveChainNumVo.setTenantCode(smartTanksVo.getTenantCode());
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(id);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);
    }


    @Override
    public R importRotaryKilnChainnum(List<ExcelImportTemplateVo.RotaryKilnVo> excelVOList) {
        //获取当前所有回转窑信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.ROTARY_KILN.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.RotaryKilnVo rotaryKilnVo = excelVOList.get(i);
            String deviceNo = rotaryKilnVo.getDeviceNo();
            String manufacturerCode = rotaryKilnVo.getManufacturerCode();
            String id = rotaryKilnVo.getId();
            String addressId = rotaryKilnVo.getAddressId();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(id);
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(addressId)) {
                    Integer addressIdInt = Integer.parseInt(addressId);
                    if (addressIdInt < DEVICE_ADDRESS_MIN || addressIdInt > DEVICE_ADDRESS_MAX) {
                        excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的机器码不在范围" + DEVICE_ADDRESS_MIN + "-" + DEVICE_ADDRESS_MAX);
                        continue;
                    }
                }
                // 校验是否存在该设备
                JSONObject gasJson = hardwareList.stream().filter(j -> j.get("baseId").equals(id)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(gasJson)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }

                // 添加设备信息
                String hardwareName = gasJson.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(id);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.ROTARY_KILN.getCode());
                saveChainNumVo.setAddressId(addressId);
                saveChainNumVo.setTenantCode(rotaryKilnVo.getTenantCode());
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(id);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);
    }


    @Override
    public R importWaterMeterChainnum(List<ExcelImportTemplateVo.WaterMeterVo> excelVOList) {
        //获取当前所有回转窑信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.WATER_METER.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.WaterMeterVo waterMeterVo = excelVOList.get(i);
            String deviceNo = waterMeterVo.getDeviceNo();
            String manufacturerCode = waterMeterVo.getManufacturerCode();
            String id = waterMeterVo.getId();
            String addressId = waterMeterVo.getAddressId();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(id);
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(addressId)) {
                    Integer addressIdInt = Integer.parseInt(addressId);
                    if (addressIdInt < DEVICE_ADDRESS_MIN || addressIdInt > DEVICE_ADDRESS_MAX) {
                        excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的机器码不在范围" + DEVICE_ADDRESS_MIN + "-" + DEVICE_ADDRESS_MAX);
                        continue;
                    }
                }
                // 校验是否存在该设备
                JSONObject gasJson = hardwareList.stream().filter(j -> j.get("baseId").equals(id)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(gasJson)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = gasJson.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(id);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.WATER_METER.getCode());
                saveChainNumVo.setAddressId(addressId);
                saveChainNumVo.setTenantCode(waterMeterVo.getTenantCode());
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(id);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);
    }

    @Override
    public R importVoiceSynthesisChainnum(List<ExcelImportTemplateVo.VoiceSynthesisVo> excelVOList) {
        //获取当前所有回转窑信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.VOICE_SYNTHESIS.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.VoiceSynthesisVo voiceSynthesisVo = excelVOList.get(i);
            String deviceNo = voiceSynthesisVo.getDeviceNo();
            String manufacturerCode = voiceSynthesisVo.getManufacturerCode();
            String id = voiceSynthesisVo.getId();
            String addressId = voiceSynthesisVo.getAddressId();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(id);
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(addressId)) {
                    Integer addressIdInt = Integer.parseInt(addressId);
                    if (addressIdInt < DEVICE_ADDRESS_MIN || addressIdInt > DEVICE_ADDRESS_MAX) {
                        excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的机器码不在范围" + DEVICE_ADDRESS_MIN + "-" + DEVICE_ADDRESS_MAX);
                        continue;
                    }
                }
                // 校验是否存在该设备
                JSONObject gasJson = hardwareList.stream().filter(j -> j.get("baseId").equals(id)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(gasJson)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = gasJson.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(id);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.VOICE_SYNTHESIS.getCode());
                saveChainNumVo.setAddressId(addressId);
                saveChainNumVo.setTenantCode(voiceSynthesisVo.getTenantCode());
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(id);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);
    }

    @Override
    public R importSoundLightAlarmChainnum(List<ExcelImportTemplateVo.SoundLightAlarmVo> excelVOList) {
        //获取当前所有回转窑信息
        List<JSONObject> hardwareList = codeBaseFactory.getHardwareList(HardwareTypeEnum.VOICE_SYNTHESIS.getCode());
        List<CodeChainnumVo.ExcelImportChainNumResult> excelImportChainNumResultList = new ArrayList<>(excelVOList.size());
        // 遍历处理数据，从第三行开始，第一第二行是标题
        for (int i = 1; i < excelVOList.size(); i++) {
            // 创建返回结果
            CodeChainnumVo.ExcelImportChainNumResult excelImportChainNumResult = new CodeChainnumVo.ExcelImportChainNumResult();
            // 设备编号
            ExcelImportTemplateVo.SoundLightAlarmVo soundLightAlarmVo = excelVOList.get(i);
            String deviceNo = soundLightAlarmVo.getDeviceNo();
            String manufacturerCode = soundLightAlarmVo.getManufacturerCode();
            String id = soundLightAlarmVo.getId();
            String addressId = soundLightAlarmVo.getAddressId();
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setBaseId(id);
            excelImportChainNumResult.setDeviceNo(deviceNo);
            excelImportChainNumResult.setFlag(false);
            try {
                if (ObjectUtil.isEmpty(deviceNo)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的设备编号为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(manufacturerCode)) {
                    excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的厂商编码为空。");
                    continue;
                }
                if (ObjectUtil.isEmpty(addressId)) {
                    Integer addressIdInt = Integer.parseInt(addressId);
                    if (addressIdInt < DEVICE_ADDRESS_MIN || addressIdInt > DEVICE_ADDRESS_MAX) {
                        excelImportChainNumResult.setMessage("错误：第" + (i + 2) + "行的机器码不在范围" + DEVICE_ADDRESS_MIN + "-" + DEVICE_ADDRESS_MAX);
                        continue;
                    }
                }
                // 校验是否存在该设备
                JSONObject gasJson = hardwareList.stream().filter(j -> j.get("baseId").equals(id)).findFirst().orElse(null);
                if (ObjectUtil.isEmpty(gasJson)) {
                    excelImportChainNumResult.setMessage("错误：该条记录的设备ID不存在。");
                    continue;
                }
                // 添加设备信息
                String hardwareName = gasJson.get("hardwareName").toString();
                CodeChainnumVo.SaveChainNumVo saveChainNumVo = new CodeChainnumVo.SaveChainNumVo();
                saveChainNumVo.setDeviceNo(deviceNo);
                saveChainNumVo.setManufacturerCode(manufacturerCode);
                saveChainNumVo.setHardwareName(hardwareName);
                saveChainNumVo.setBaseId(id);
                saveChainNumVo.setHardwareType(HardwareTypeEnum.SOUND_LIGHT_ALARM.getCode());
                saveChainNumVo.setAddressId(addressId);
                saveChainNumVo.setTenantCode(soundLightAlarmVo.getTenantCode());
                // 基本校验通过，可以存储查询出来的基本返回信息，之前是存入列表的查询信息
                excelImportChainNumResult.setBaseId(id);
                excelImportChainNumResult.setHardwareName(hardwareName);
                try {
                    Boolean flag = codeChainnumService.saveCodeChainNumInfo(saveChainNumVo);
                    if (!flag) {
                        excelImportChainNumResult.setMessage("错误：添加设备失败");
                        continue;
                    }
                } catch (Exception e) {
                    excelImportChainNumResult.setMessage("错误：" + e.getMessage());
                    continue;
                }
                // 添加成功
                excelImportChainNumResult.setFlag(true);
                excelImportChainNumResult.setMessage("成功");
            } finally {
                excelImportChainNumResultList.add(excelImportChainNumResult);
            }
        }
        return R.ok(excelImportChainNumResultList);
    }
}
